//给定一个节点数为n的无序单链表，对其按升序排序。
import java.util.*;
public class Solution {

      public class ListNode {
        int val;
        ListNode next = null;
        public ListNode(int val) {
          this.val = val;
        }
      }
    public ListNode sortInList0 (ListNode head) {
        ListNode dummy = new ListNode(-1);
        ListNode fast = head;
        ListNode slow = head;

        if(head == null){
            return null;
        }
        while(fast != null){
            ListNode pre = dummy.next;
            while(slow.val <= fast.val){
                slow = slow.next;
                fast = fast.next;
            }
            slow.next = slow.next.next;
            fast.next = pre.next;

            pre.next = fast;
            slow = slow.next;
            fast = fast.next;
        }
        return dummy.next;
    }
    public ListNode sortInList (ListNode head) {
          ArrayList<Integer> list = new ArrayList<>();
          ListNode p = head;
          while(p != null){
              list.add(p.val);
              p = p.next;
          }
          p = head;
          Collections.sort(list);
          //将顺序表元素添加到链表中
          for(int i = 0; i < list.size(); i++){
              p.val = list.get(i);
              p = p.next;
          }
          return head;
    }
}